<?php
/**
* Plugin that helps to control dates.
*
* @author Oliver Leisalu
* @package DomAr
* @subpackage Plugin
*/
class DomArTranslatable extends DomArPlugin {
	/**
	 * @var Array
	 */
	public $languages = array();
	/**
	 * @param String $var
	 * @param String $value
	 */
	public function __set( $var, $value ) {
		$this->languages[strtolower($var)] = (string)$value;
	}
	/**
	 * @param String $var
	 */
	public function __get( $var ) {
		return $this->languages[strtolower($var)];
	}
	/**
	 * @return String
	 */
	public function __toString() {
		$key = DomLanguage::getk();
		if( !$key )
			throw new RuntimeException('Language key cannot be loaded!');
		
		return (string)$this->languages[$key];
	}
}



/**
* @author Oliver Leisalu
* @package DomAr
* @subpackage Plugin
*/
class DomArProperty_DomArTranslatable extends DomArProperty_Plugin implements IDomArPropertyAlterTableListener,IDomArPropertyInsertListener,IDomArPropertyUpdateListener,IDomArPropertyBeforeSetListener {
	
	protected $defaultValue;
	
	public function __construct( $name, $defaultValue = NULL ) {
		parent::__construct( $name );
		$this->defaultValue = $defaultValue;
	}
	
	public function init( DomArObject $object, array &$values ) {
		return (string)$values[$this->name];
	}
	
	public function load( DomArObject $object, &$propertyValue ) {
		
		$d = new DomArDate( ($propertyValue !== NULL ? $propertyValue : ($this->defaultValue ? $this->defaultValue : NULL)) );
		$d->changeParent( $object, $this );
		return $d;
	}
	
	public function beforeSet( DomArObject $object, &$oldPropertyValue, &$newPropertyValue ) {
		if( is_object($newPropertyValue) ) {
			$newPropertyValue->changeParent( $object, $this );
		} else {
			$newPropertyValue = new DomArDate( $newPropertyValue);
			$newPropertyValue->changeParent( $object, $this );
		}
	}
	
	public function isLoaded( DomArObject $object, &$propertyValue ) {
		return is_object($propertyValue);
	}
	
	public function validate( DomArObject $object, &$propertyValue, array &$customParams = array() ) {
		return (is_object($propertyValue) && get_class($propertyValue) == 'DomArDate' or is_string($propertyValue) && DomArDate::validate($propertyValue) or is_numeric($propertyValue)) ? 0 : 1;
	}
	
	public function insert( DomArObject $object, &$propertyValue, DomArValueSet $set  ) {
		$propertyValue = $object->{$this->name};
		if( is_object($propertyValue) )
			$value = ($propertyValue->isNull() ? 'NULL' : '\''.$propertyValue->date('Y-m-d').'\'');
		else
			$value = ($propertyValue == '' ? 'NULL' : '\''.$propertyValue.'\'');
			
		$set->set( $this->name, $value );
	}
	
	public function update( DomArObject $object, &$propertyValue, DomArValueSet $set ) {
		$set->set( $this->name, ($propertyValue->isNull() ? 'NULL' : '\''.$propertyValue->date('Y-m-d').'\'') );
	}
	
	public function alterTable( DomArTable $table ) {
		return $table->changeColumn( $this->name, 'date', true );
	}
}




class Translatable {
	
	public $value;
	
	
	public function __get($var) {
		
	}
	
	
}


class Sample extends DomArObject {
	/**
	 * @orm translatable text
	 */
	public $title;
	/**
	 * @orm translatable longtext
	 */
	public $contents;
	/**
	 * @orm plugin DomArDateTime(now)
	 */
	public $datetime;
	
}

$sample = new  Sample();
$sample->title->est = 'Tere!';
$sample->title->eng = 'Hello!';
$sample->contents->est = 'Sisu';
$sample->contents->eng = 'Contents';
$sample->datetime = 'now';

// Current default language is Est

echo $sample->title; // Tere!

?>